package com.cqndt.site.controller;

import com.cqndt.site.service.back.SysUserService;
import com.cqndt.site.util.Result;
import com.cqndt.site.vo.SysUserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:用户管理
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping(value="list")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "userName", value = "用户名", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "page", value = "页数", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "limit", value = "每页条数", defaultValue = "10", paramType = "query")
    })
    public Result listTabUser(SysUserVo sysUserVo) {
        PageHelper.startPage(sysUserVo.getPage(),sysUserVo.getLimit());
        PageInfo<SysUserVo> pageInfo = new PageInfo<SysUserVo>(sysUserService.listByCondition(sysUserVo));
        Result result = new Result();
        result.setCount((int)pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @PostMapping("save")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "userName", value = "用户名", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "password", value = "密码", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "nickName", value = "昵称", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "aduitType", value = "赋予的权限：1后台 2前台 4app；如壳多选如选了1和2那么值为3", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "remark", value = "备注", defaultValue = "", paramType = "query")
    })
    public Result save(SysUserVo sysUserVo){
        return sysUserService.addUser(sysUserVo);
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "用户Id", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "userName", value = "用户名", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "nickName", value = "昵称", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "aduitType", value = "赋予的权限：1后台 2前台 4app；如壳多选如选了1和2那么值为3", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "remark", value = "备注", defaultValue = "", paramType = "query")
    })
    public Result update(SysUserVo sysUserVo){
        return sysUserService.updateUser(sysUserVo);
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "ids", value = "用户Id,多条逗号隔开", defaultValue = "", paramType = "query")
    })
    public Result delete(String ids){
        return sysUserService.deleteUser(ids);
    }

    /**
     * 修改密码
     */
    @PostMapping("upPassword")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "oldPass", value = "原始密码", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "newPass", value = "新密码", defaultValue = "", paramType = "query")
    })
    public Result upPassword(SysUserVo sysUserVo){
        Result result = new Result();
        try {
            //Integer id = getUserId();
            Integer id = 42;
            result = sysUserService.upPassword(id,sysUserVo.getOldPass(),sysUserVo.getNewPass());
        } catch (Exception e) {
            e.printStackTrace();
            return result.failure(-1,"操作失败");
        }
        return result;
    }
    /**
     * 重置密码
     */
    @PostMapping("resetPassword")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "重置用户id", defaultValue = "", paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "newPass", value = "重置密码", defaultValue = "", paramType = "query"),
    })
    public Result resetPassword(SysUserVo sysUserVo){
        Result result = new Result();
        try {
            result = sysUserService.resetPass(sysUserVo.getId(),sysUserVo.getNewPass());
        } catch (Exception e) {
            e.printStackTrace();
            return result.failure(-1,"操作失败");
        }
        return result;
    }


}
